from rest_framework.viewsets import ReadOnlyModelViewSet
from meiduo_admin.utils import MyNumbPage
from orders.models import OrderInfo
from .serializers import OrderInfoSerializer
from rest_framework.decorators import action
from rest_framework.response import Response


class OrderViewSet(ReadOnlyModelViewSet):
    serializer_class = OrderInfoSerializer

    pagination_class = MyNumbPage

    def get_queryset(self):
        key_word = self.request.query_params.get('keyword')
        if key_word:
            return OrderInfo.objects.order_by('-order_id').filter(order_id__contains=key_word).all()
        return OrderInfo.objects.order_by('-order_id').all()

    @action(methods=['put'], detail=True)
    def status(self, request, pk):
        status = request.data.get('status')
        OrderInfo.objects.filter(order_id=pk).update(status=status)

        return Response(status=200)
